package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.PT;
import com.itheima.pojo.Psychoanalysis;
import com.itheima.service.PsychoanalysisService;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;


/**
 * @author jiabo
 * @Date 9.12
 */
@RestController
@RequestMapping("psychoanalysis")
public class PsychoanalysisController {

    @Reference(version = "1.0.0")
    private PsychoanalysisService psychoanalysisService;

    /**
     * 新增检查项
     * @param
     * @param
     * @return  新增操作结果
     */
    @RequestMapping("add")
    // @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result add(@RequestBody Psychoanalysis psychoanalysis) throws ParseException {
//        if(errors.hasErrors()){
//            StringBuilder msg = new StringBuilder();
//            List<FieldError> fieldErrors = errors.getFieldErrors();
//            for (int i = 0; i < fieldErrors.size(); i++) {
//                if(i!=fieldErrors.size()-1) {
//                    msg.append(fieldErrors.get(i).getDefaultMessage()).append(",");
//                }else {
//                    msg.append(fieldErrors.get(i).getDefaultMessage()).append("!");
//                }
//            }
//            return new Result(false, msg.toString());
//        }
        //调用业务层添加方法
        psychoanalysisService.add(psychoanalysis);
        //返回操作结果
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    /**
     * 编辑
     * @param psychoanalysis 检查组实体类
     * @return  编辑操作的结果
     */
    @RequestMapping("update")
    // @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody Psychoanalysis psychoanalysis){
        //调用业务层修改方法
        psychoanalysisService.update(psychoanalysis);
        //返回操作结果
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 根据ID查询检查组
     * @param id    检查组ID
     * @return  返回结果
     */
    @GetMapping("findById/{id}")
   //  @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(@PathVariable int id){
        PT psList = psychoanalysisService.findById(id);
        return new Result(true,"查询成功",psList);
    }

    /**
     * 查询所有的检查组
     * @return
     */
    @Cacheable(expire = 600)
    @PostMapping("findAll")
    public Result findAll(){
        List<PT> PTList = psychoanalysisService.findAll();
        System.out.println("这里是查询所有检查组，我从数据库查询了！！！");
        return new Result(true,"查询成功",PTList);
    }

    /**
     * 删除
     * @param id 要删除的检查组的ID
     * @return  编辑操作的结果
     */
    @GetMapping("delete/{id}")
   // @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        psychoanalysisService.delete(id);
        //返回操作结果
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询方法
     * @param queryPageBean 分页参数实体类
     * @return  分页查询结果，总条数与当前页数据
     */
    @PostMapping("findPage")
   // @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = psychoanalysisService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
}
